package com.foreveross.testproxy;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.foreveross.constant.Constants;
import com.foreveross.testproxy.timer.RedisTianxunTimer;
import com.foreveross.util.QueueUtil;
import com.foreveross.util.SpringHelper;
import com.foreveross.util.cfg.ConfigContainer;
import com.foreveross.util.cfg.defaults.DefaultPropertiesLoader;
import com.foreveross.util.redis.JRedisImpl;
import com.foreveross.util.redis.RedisService;

public class TestProxyIPMain {

	private static final Logger LOG = LoggerFactory
			.getLogger(TestProxyIPMain.class);
	private static RedisService redisServive = new JRedisImpl();

	public static void main(String[] args) {

		// 配置文件注册.
		LOG.info(String.format("配置文件初始化..."));
		ConfigContainer.getInstance().register(DefaultPropertiesLoader.class);
		// System.out.println(DefaultPropertiesLoader.getClientId());

		// 初始化Spring.
		LOG.info(String.format("Spring初始化..."));
		SpringHelper.setClassPathXmlApplicationContext("spring/root.xml");

		LOG.info(String.format("Spring初始化成功..."));
		String clientId = UUID.randomUUID().toString();
		//redisServive.recordProxyIPLog(clientId, Constants.CHANNEL_TIANXUN, 0);

		//Reids 线程从天巡里面监听如果有IP 写入池
		new RedisTianxunTimer(QueueUtil.getTianxunQueue());
		
		// 检测IP
		proxyIPTest(clientId);
		
		

	}

	public static void proxyIPTest(String clientId) {
		int redisLeastSize = Integer.parseInt(DefaultPropertiesLoader
				.getReidsTianxunPoolMin());
		// //天巡渠道 计时器 检测IP
		for (int i = 0; i < 100000; i++) {
			long currentRedisHotSize = redisServive
					.countHotpoolSize(Constants.CHANNEL_TIANXUN);
			if (currentRedisHotSize < redisLeastSize) {
				// 多线程调用
				try {
					boolean flag = ProxyIpTianxunProcess
							.proxyIpTestProcess(clientId);

				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			LOG.info(String.format("执行..." + i));

		}
	}

}
